﻿using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Platform.OAuth2;
using MiniFox.Platform.OAuth2.Providers;

namespace OAuth2.Data
{
    /// <summary>
    /// 
    /// </summary>
    [AutoResolve]
    public class PassportTokenProvider : EntityDataProvider<PlatformDbContext>, IPassportTokenProvider
    {
        public PassportToken GetPassportToken(string authTicketID)
        {
            var sys_passportToken = this.CurrentDbContext.SysPassportToken.SingleOrDefault(p => p.TicketId == authTicketID);
            if (sys_passportToken == null)
                return null;
            return PassportToken.Create(sys_passportToken.TicketId, sys_passportToken.CreateTime, sys_passportToken.RenewTime, sys_passportToken.Duration);
        }

        public void DisablePassportToken(PassportToken passportToken)
        {
            if (passportToken == null)
                return;
            SysPassportToken sys_token = this.CurrentDbContext.SysPassportToken.SingleOrDefault(t => t.TicketId == passportToken.TicketID);
            if (sys_token == null)
            {
                return;
            }
            this.Delete(sys_token);

            try
            {
                SysSignLog sys_signInLog = this.CurrentDbContext.SysSignLog.SingleOrDefault(u => u.TicketId == passportToken.TicketID);
                sys_signInLog.SignOffTime = DateTime.Now;
                this.Update(sys_signInLog);
            }
            catch
            {

            }
        }

    }
}
